package org.browntooth.app;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.browntooth.app.bluetooth.Bluetooth;
import org.browntooth.app.bluetooth.BluetoothDeviceConnection;
import org.browntooth.app.bluetooth.BluetoothEventListener;
import org.browntooth.app.communicator.BrowntoothCommunicator;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

public class BrowntoothActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		try {
			init();
		} catch (Exception e) {
			Log.e("Browntooth", "ERROR: " + e.getMessage(), e);
		}
	}

	private boolean isInitialized = false;

	private void init() {
		if (isInitialized) {
			return;
		}

		try {
			Bluetooth bluetooth = new Bluetooth(this);
			BrowntoothCommunicator communicator = new BrowntoothCommunicator(
					bluetooth);
			communicator.start();

			Log.d("Browntooth", "!!!");
		} catch (Exception e) {
			Log.e("Browntooth", "ERROR: " + e.getMessage(), e);
		}

		isInitialized = true;
	}

	private void init2() throws IOException {
		new Thread() {
			public void run() {
				Looper.prepare();
				
				BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

				BluetoothServerSocket serverSocket;
				try {
					serverSocket = adapter
							.listenUsingRfcommWithServiceRecord("Bla",
									BrowntoothCommunicator.BROWNTOOTH_SERVICE_UUID);
					
					Log.d("Browntooth", "A");
					serverSocket.accept();
					Log.d("Browntooth", "B");
				} catch (IOException e) {
					Log.d("Browntooth", "C");
				}

			}
		}.start();
	}

	private class Listener implements BluetoothEventListener {
		public void connectedToDivece(BluetoothDeviceConnection connection) {
			Log.d("Browntooth", "CONNECTED: " + connection.getServiceUuid());

			try {
				OutputStream os = connection.getOutputStream();
				InputStream is = connection.getInputStream();

				for (int i = 0; i < 10; i++) {
					String str = "Moo " + i + "!\n";
					os.write(str.getBytes());
				}

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				byte[] data = new byte[is.available()];

				is.read(data);

				String recieved = new String(data);

				Log.d("Browntooth", "RECIEVED: " + recieved);
			} catch (IOException e) {
				Log.e("Browntooth", "IO ERROR: " + e.getMessage(), e);
			}
		}

		public void connectionToDiveceFailed(BluetoothDevice device,
				UUID serviceUuid, Exception exception) {
			Log.e("Browntooth", "connectionToDiveceFailed: "
					+ exception.getMessage(), exception);
		}

		public void deviceDiscovered(BluetoothDevice device) {
			Log.d("Browntooth", "DISCOVERED: " + device.getAddress() + " : "
					+ device.getName());
		}

		public void remoteDeviceConnectionFailed(UUID serviceUuid,
				Exception exception) {
			Log.e("Browntooth", "remoteDeviceConnectionFailed: "
					+ exception.getMessage(), exception);
		}
	}
}